package com.demo.core.dao;

import com.demo.base.page.IPagableDTO;
import com.demo.base.page.PagableDTO;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.demo.base.model.FullTimeJob;
import com.demo.base.page.IPagableDTO;
import com.demo.base.page.PagableDTO;

/**
 * Created by jiandandia on 2017/4/5.
 */
@Repository
public class FullTimeJobDao extends BaseDao{
    public FullTimeJob newOrupdateFullTimeJob(FullTimeJob fullTimeJob) throws Exception {
        return saveEntity(fullTimeJob);
    }

    public FullTimeJob getFullTimeJobById(String fullTimeJobId) throws Exception {
        FullTimeJob retval = null;
        try {
            if (!StringUtils.isEmpty(fullTimeJobId)) {
                retval = (FullTimeJob) this.get(FullTimeJob.class, fullTimeJobId);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception(e);
        }
        return retval;
    }

    public IPagableDTO<FullTimeJob> getFullTimeJobByQuery(FullTimeJob fullTimeJob, IPagableDTO pagableDTO) {
        IPagableDTO<FullTimeJob> retval = new PagableDTO<FullTimeJob>();
        try {
            Session session = getSession();
            Criteria c = session.createCriteria(FullTimeJob.class);
            //兼职类型
            if(null != fullTimeJob.getType()){
                c.add(Restrictions.eq("type", fullTimeJob.getType()));
            }
            //工作性质
            if(null != fullTimeJob.getJobNature()){
                c.add(Restrictions.eq("jobNature", fullTimeJob.getJobNature()));
            }
            //工作类型
            if(null != fullTimeJob.getJobNature()){
                c.add(Restrictions.eq("workType", fullTimeJob.getWorkType()));
            }
            //工作区域
            if(null != fullTimeJob.getJobNature()){
                c.add(Restrictions.eq("workArea", fullTimeJob.getWorkArea()));
            }
            //工作环境
            if(null != fullTimeJob.getJobNature()){
                c.add(Restrictions.eq("workEnvironment", fullTimeJob.getWorkEnvironment()));
            }

            //后期加其他查询条件
            Long totalRow = (Long) c.setProjection(Projections.rowCount()).uniqueResult();
            retval.setTotalRow(totalRow.intValue());
            c.setProjection(null);
            if (null != pagableDTO) {
                c.setFirstResult(pagableDTO.getStartRow());
                c.setFetchSize(pagableDTO.getPageSize());
                c.setMaxResults(pagableDTO.getPageSize());
            }
            retval.setData(c.list());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return retval;
    }

    public void delteFullTimeJobById(String id) throws Exception{
        Session s = getSession();
        try {
            s.delete(s.get(FullTimeJob.class, id));
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception(e);
        }
    }

    public boolean addBrowsingNumber(String noticeId) {
        try {
            String hql = "update FullTimeJob set browsingNumber=browsingNumber + 1 "
                    + " where id=?";
            hExecuteUpdate(hql, new Object[]{noticeId});
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public boolean addFollowersNumber(String noticeId) {
        try {
            String hql = "update FullTimeJob set followersNumber=followersNumber + 1 "
                    + " where id=?";
            hExecuteUpdate(hql, new Object[]{noticeId});
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public boolean addManEnterNumber(String id) {
        try {
            String hql = "update FullTimeJob set manEnter=manEnter + 1 "
                    + " where id=?";
            hExecuteUpdate(hql, new Object[]{id});
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public boolean addWoManEnterNumber(String id) {
        try {
            String hql = "update FullTimeJob set womanEnter=womanEnter + 1 "
                    + " where id=?";
            hExecuteUpdate(hql, new Object[]{id});
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}
